namespace Adge.Common
{
    
    using System;
    using System.Data;
    using System.IO;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Text.RegularExpressions;
    using System.Threading;
    using System.Xml.Serialization;

    public class ConfigFactory
    {
        static ConfigFactory()
        {
            ConfigFactory.syncRoot = new object();
        }

        public static bool BetweenTime(string timelist)
        {
            string text1 = "";
            return ConfigFactory.BetweenTime(timelist, out text1);
        }

        public static bool BetweenTime(string timelist, out string vtime)
        {
            if (!timelist.Equals(""))
            {
                string[] textArray1 = Utils.SplitString(timelist, "\n");
                if (textArray1.Length > 0)
                {
                    string text1 = "";
                    int num1 = 0;
                    string text2 = "";
                    int num2 = 0;
                    foreach (string text3 in textArray1)
                    {
                        if (Regex.IsMatch(text3, "^((([0-1]?[0-9])|(2[0-3])):([0-5]?[0-9])-(([0-1]?[0-9])|(2[0-3])):([0-5]?[0-9]))$"))
                        {
                            text1 = text3.Substring(0, text3.IndexOf("-"));
                            num1 = Utils.StrDateDiffMinutes(text1, 0);
                            text2 = Utils.CutString(text3, text3.IndexOf("-") + 1, text3.Length - (text3.IndexOf("-") + 1));
                            num2 = Utils.StrDateDiffMinutes(text2, 0);
                            if ((((num1 > 0) && (num2 < 0)) || ((num1 < 0) && (num2 < 0))) || (((num1 > 0) && (num2 > 0)) && (num2 > num1)))
                            {
                                vtime = text3;
                                return true;
                            }
                        }
                    }
                }
            }
            vtime = "";
            return false;
        }

        private static ConfigInfo CheckConfigFile()
        {
            CSCache cache1 = CSCache.GetCacheService();
            string[] textArray1 = new string[] { Utils.GetMapPath("config/general.config") };
            ConfigInfo info1 = new ConfigInfo();
            DataSet set1 = new DataSet();
            set1.ReadXml(textArray1[0]);
            Type type1 = info1.GetType();
            foreach (PropertyInfo info2 in type1.GetProperties())
            {
                if (set1.Tables[0].Columns.Contains(info2.Name))
                {
                    if (info2.PropertyType.Name != "Int32")
                    {
                        type1.InvokeMember(info2.Name, BindingFlags.SetProperty, null, info1, new object[] { set1.Tables[0].Rows[0][info2.Name].ToString() });
                    }
                    else
                    {
                        type1.InvokeMember(info2.Name, BindingFlags.SetProperty, null, info1, new object[] { Convert.ToInt32(set1.Tables[0].Rows[0][info2.Name].ToString()) });
                    }
                }
            }
            cache1.AddObject("/Forum/Setting", info1, textArray1);
            return ConfigFactory.Serialiaze(info1, textArray1[0]);
        }

        public static ConfigInfo Deserialize(string configFilePath)
        {
            XmlSerializer serializer1 = new XmlSerializer(typeof(ConfigInfo));
            Stream stream1 = new FileStream(configFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            return (ConfigInfo) serializer1.Deserialize(stream1);
        }

        public static ConfigInfo GetConfig()
        {
            CSCache cache1 = CSCache.GetCacheService();
            ConfigInfo info1 = cache1.RetrieveObject("/Forum/Setting") as ConfigInfo;
            if (info1 == null)
            {
                string[] textArray1 = new string[] { Utils.GetMapPath("config/general.config") };
                info1 = ConfigFactory.Deserialize(Utils.GetMapPath("config/general.config"));
                cache1.AddObject("/Forum/Setting", info1, textArray1);
            }
            return info1;
        }

        public static float GetCreditsTax()
        {
            CSCache cache1 = CSCache.GetCacheService();
            string text1 = cache1.RetrieveObject("/Forum/Scoreset/CreditsTax") as string;
            if (text1 == null)
            {
                DataSet set1 = new DataSet();
                set1.ReadXml(Utils.GetMapPath( "/config/scoreset.config"));
                text1 = set1.Tables["formula"].Rows[0]["creditstax"].ToString();
                cache1.AddObject("/Forum/CreditsTax", text1);
            }
            return float.Parse(text1);
        }

        public static int GetCreditsTrans()
        {
            CSCache cache1 = CSCache.GetCacheService();
            string text1 = cache1.RetrieveObject("/Forum/Scoreset/CreditsTrans") as string;
            if (text1 == null)
            {
                DataSet set1 = new DataSet();
                set1.ReadXml(Utils.GetMapPath( "/config/scoreset.config"));
                text1 = set1.Tables["formula"].Rows[0]["creditstrans"].ToString();
                cache1.AddObject("/Forum/CreditsTrans", text1);
            }
            return short.Parse(text1);
        }

        public static int GetDefaultTemplateID()
        {
            CSCache cache1 = CSCache.GetCacheService();
            string text1 = cache1.RetrieveObject("/Forum/DefaultTemplateID") as string;
            if (text1 != null)
            {
                return Utils.StrToInt(text1, 1);
            }
            int num1 = ConfigFactory.GetConfig().Templateid;
            cache1.AddObject("/Forum/DefaultTemplateID", num1.ToString());
            return num1;
        }

        public static int GetExchangeMinCredits()
        {
            CSCache cache1 = CSCache.GetCacheService();
            string text1 = cache1.RetrieveObject("/Forum/Scoreset/ExchangeMinCredits") as string;
            if (text1 == null)
            {
                DataSet set1 = new DataSet();
                set1.ReadXml(Utils.GetMapPath( "/config/scoreset.config"));
                text1 = set1.Tables["formula"].Rows[0]["exchangemincredits"].ToString();
                cache1.AddObject("/Forum/ExchangeMinCredits", text1);
            }
            return Convert.ToInt16(text1);
        }

        public static int GetMaxChargeSpan()
        {
            CSCache cache1 = CSCache.GetCacheService();
            string text1 = cache1.RetrieveObject("/Forum/Scoreset/MaxChargeSpan") as string;
            if (text1 == null)
            {
                DataSet set1 = new DataSet();
                set1.ReadXml(Utils.GetMapPath( "/config/scoreset.config"));
                text1 = set1.Tables["formula"].Rows[0]["maxchargespan"].ToString();
                cache1.AddObject("/Forum/MaxChargeSpan", text1);
            }
            return Convert.ToInt16(text1);
        }

        public static int GetMaxIncPerTopic()
        {
            CSCache cache1 = CSCache.GetCacheService();
            string text1 = cache1.RetrieveObject("/Forum/Scoreset/MaxIncPerThread") as string;
            if (text1 == null)
            {
                DataSet set1 = new DataSet();
                set1.ReadXml(Utils.GetMapPath( "/config/scoreset.config"));
                text1 = set1.Tables["formula"].Rows[0]["maxincperthread"].ToString();
                cache1.AddObject("/Forum/MaxIncPerThread", text1);
            }
            return Convert.ToInt16(text1);
        }

        public static string GetScoreCalFormula()
        {
            return ConfigFactory.GetScoreCalFormula( "/config/scoreset.config");
        }

        public static string GetScoreCalFormula(string configFilePath)
        {
            CSCache cache1 = CSCache.GetCacheService();
            string text1 = cache1.RetrieveObject("/Forum/Scoreset") as string;
            if (text1 == null)
            {
                DataSet set1 = new DataSet();
                set1.ReadXml(Utils.GetMapPath(configFilePath));
                text1 = set1.Tables["formula"].Rows[0]["formulacontext"].ToString();
                cache1.AddObject("/Forum/Scoreset", text1);
            }
            return text1;
        }

        public static DataTable GetScorePaySet()
        {
            CSCache cache1 = CSCache.GetCacheService();
            DataTable table1 = cache1.RetrieveObject("/Forum/ScorePaySet") as DataTable;
            if (table1 != null)
            {
                return table1;
            }
            DataTable table2 = ConfigFactory.GetScoreSet();
            DataTable table3 = new DataTable();
            table3.Columns.Add("id", Type.GetType("System.Int32"));
            table3.Columns.Add("name", Type.GetType("System.String"));
            table3.Columns.Add("rate", Type.GetType("System.Single"));
            for (int num1 = 1; num1 < 9; num1++)
            {
                if ((table2.Rows[0]["extcredits" + num1.ToString()].ToString().Trim() != "") && (table2.Rows[2]["extcredits" + num1.ToString()].ToString() != "0"))
                {
                    DataRow row1 = table3.NewRow();
                    row1["id"] = num1;
                    row1["name"] = table2.Rows[0]["extcredits" + num1.ToString()].ToString().Trim();
                    row1["rate"] = Utils.StrToFloat(table2.Rows[2]["extcredits" + num1.ToString()].ToString(), 0f);
                    table3.Rows.Add(row1);
                }
            }
            cache1.AddObject("/Forum/ScorePaySet", table3);
            return table3;
        }

        public static DataTable GetScoreSet()
        {
            CSCache cache1 = CSCache.GetCacheService();
            DataTable table1 = cache1.RetrieveObject("/Forum/ScoreSet") as DataTable;
            if (table1 != null)
            {
                return table1;
            }
            DataSet set1 = new DataSet();
            set1.ReadXml(Utils.GetMapPath( "config/scoreset.config"));
            string[] textArray1 = new string[] { Utils.GetMapPath( "config/scoreset.config") };
            cache1.AddObject("/Forum/ScoreSet", set1.Tables[0], textArray1);
            return set1.Tables[0];
        }        

        public static int GetTransferMinCredits()
        {
            CSCache cache1 = CSCache.GetCacheService();
            string text1 = cache1.RetrieveObject("/Forum/Scoreset/TransferMinCredits") as string;
            if (text1 == null)
            {
                DataSet set1 = new DataSet();
                set1.ReadXml(Utils.GetMapPath( "/config/scoreset.config"));
                text1 = set1.Tables["formula"].Rows[0]["transfermincredits"].ToString();
                cache1.AddObject("/Forum/TransferMinCredits", text1);
            }
            return Convert.ToInt16(text1);
        }

        public static string[] GetValidScoreName()
        {
            CSCache cache1 = CSCache.GetCacheService();
            string[] textArray1 = cache1.RetrieveObject("/Forum/ValidScoreName") as string[];
            if (textArray1 == null)
            {
                textArray1 = new string[9];
                textArray1[0] = "";
                DataTable table1 = ConfigFactory.GetScoreSet();
                for (int num1 = 1; num1 < 9; num1++)
                {
                    if (table1.Rows[0]["extcredits" + num1.ToString()].ToString() != "")
                    {
                        textArray1[num1] = table1.Rows[0]["extcredits" + num1.ToString()].ToString();
                    }
                    else
                    {
                        textArray1[num1] = "";
                    }
                }
                table1.Dispose();
                cache1.AddObject("/Forum/ValidScoreName", textArray1);
            }
            return textArray1;
        }

        public static ConfigInfo Serialiaze(ConfigInfo __configinfo, string configFilePath)
        {
            lock (ConfigFactory.syncRoot)
            {
                XmlSerializer serializer1 = new XmlSerializer(typeof(ConfigInfo));
                Stream stream1 = new FileStream(configFilePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                serializer1.Serialize(stream1, __configinfo);
                stream1.Close();
            }
            return __configinfo;
        }

        private static void Serializer_UnknownElement(object sender, XmlElementEventArgs e)
        {
            ConfigFactory.CheckConfigFile();
        }

        public static bool SetIpDenyAccess(string denyipaccess)
        {
            bool flag1;
            object obj1;
            Monitor.Enter(obj1 = ConfigFactory.syncRoot);
            try
            {
                ConfigInfo info1 = ConfigFactory.GetConfig();
                info1.Ipdenyaccess = info1.Ipdenyaccess + "\n" + denyipaccess;
                ConfigFactory.Serialiaze(info1, Utils.GetMapPath( "config/general.config"));
                flag1 = true;
            }
            catch
            {
                flag1 = false;
            }
            finally
            {
                Monitor.Exit(obj1);
            }
            return flag1;
        }


        private static object syncRoot;
    }
}

